var useridn = '';
function logoutnow(){
    layui.layer.confirm('确定要退出登录吗？', {
        btn: ['是','否'] 
    }, function(){
        sessionStorage.removeItem("exam_userid");
        sessionStorage.removeItem("exam_username");
        location.href = "juslr/logout.php";
    });
}
var userid = sessionStorage.getItem("exam_userid");
if(userid === null || userid === ''){
    userid = get_userid_2();
}
var examtion_putplace = document.getElementById('examtion_putpl');
function gettochangestatus(statusidto){
    layui.layer.confirm('确定要修改吗？', {
        btn: ['是','否'] 
    }, function(){
        const formData = new FormData();
        formData.append('exmatid', statusidto);
        fetch('exam/tochangestatsus.php', {
            method: 'POST',
            body: formData
        })
        .then(function (response) {
            return response.json();
        }).then(function (data) {
            if(data.status === 200){
                Qmsg.success('修改成功');
                setTimeout(function(){ location.reload(); }, 1000);
            }else{
                Qmsg.error('修改失败');
            }
            layer.closeAll();
        });
    });  
}
function getallexamtion(){
    fetch('./exam/getall.php?userid='+userid)
    .then(function (response) {
        return response.json();
    }).then(function (data) {
        if(data.code === 200){
            yhload.loadqr_hide('examtion_putpl');
            data_result = data.result;
            data_result.forEach(godin => {
                const table = document.createElement('table');
                var headerRow = document.createElement('tr');
                ['卷子标题', '创建时间', '状态', '操作'].forEach(function(text) {
                    var th = document.createElement('th');
                    th.textContent = text;
                    headerRow.appendChild(th);
                });
            table.appendChild(headerRow);
            var dataRow = document.createElement('tr');
            if(godin.exmatzt === 1){
                var exmatztss = '已发布';
                var exmatztssw = '回收考卷'
            }else{
                var exmatztss = '未发布';
                var exmatztssw = '发布考卷'
            }
            [godin.exmatname, godin.exmatcreatetime, exmatztss, 
                `<a onclick="survey_seeino('${godin.exmatid}','${godin.exmatname}');">查看</a> | <a onclick="survey_xchbj('${godin.exmatid}','${godin.exmatname}');">编辑</a> | <a onclick="gettochangestatus('${godin.exmatid}');">${exmatztssw}</a> | <a onclick="survey_toshareorsee('${godin.exmatid}');">分享链接</a> | <a onclick="survey_delone('${godin.exmatid}');">删除</a>`
            ].forEach(function(textOrHtml, index) {
            var td = document.createElement('td');
            if (index === 3) {
                td.innerHTML = textOrHtml;
            } else {
                td.textContent = textOrHtml;
            }
            dataRow.appendChild(td);
});
            table.appendChild(dataRow);
            table.id = godin.exmatid;
            examtion_putplace.appendChild(table);
            table.classList.add('slide-in-from-right');
            });
        }else{
            Qmsg.error('异常的模块处理');
        }
    });
}
function isPageLoaded() {
    return document.readyState == "complete";
}
function timecaparevip(times){
    var currentTime = new Date();
    var futureDate = new Date(times);
    if (futureDate > currentTime) {
        return true;
    }
}

function loadingaboutmyself(){
    fetch('./exam/getallbym.php?userid='+userid)
    .then(function (response) {
        return response.json();
    }).then(function (data) {
        if(data.code === 200){
            var resultvip = data.result;
            resultvip = resultvip[0];
            useridn = resultvip.id;
            sessionStorage.setItem("exam_userid_forget",useridn);
            if(timecaparevip(resultvip.vip)){
                document.getElementById('vips').className = 'label-vip'
                document.getElementById('vipselse').style.display = 'none';
            }
            const kzts = document.getElementById('smzt_namereal');
            var facetoauth = ifopenauthface();
            if(facetoauth === 'open'){
                if(resultvip.smrz === null || resultvip.smrz === ''){
                    layer.open({
                        closeBtn: 0,
                        content: '请先进行实名认证'
                        ,btn: ['立即认证','退出登录']
                        ,yes: function(index, layero){
                            layer.closeAll();
                            open_realhumantoauth();
                        }
                        ,btn2: function(index, layero){
                            logoutnow();
                        }
                    });
                }else{
                    kzts.innerText = '已实名认证';
                    kzts.style = '';
                    kzts.onclick = function(){
                        layer.alert('若要修改实名认证信息，请联系管理员');
                    }
                }
            }
        }else{
            Qmsg.error('异常的模块处理');
        }})
}

if (!isPageLoaded()) {
    window.onload = function () {
        document.getElementById('vips').className = 'label-notvip'
        yhload.loadqr_show('examtion_putpl');
        getallexamtion();
        loadingaboutmyself();
    }
}
function survey_delone(deltheid){
    layui.layer.confirm('确定要删除吗？', {
        btn: ['是','否'] 
    }, function(){
        const formData = new FormData();
        formData.append('exmatid', deltheid);
        fetch('exam/deloneexmation.php', {
            method: 'POST',
            body: formData
        })
        .then(function (response) {
            return response.json();
        }).then(function (data) {
            if(data.status === 200){
                Qmsg.success('删除成功');
                document.getElementById(deltheid).remove();
            }else{
                Qmsg.error('删除失败');
            }
            layer.closeAll();
        });
    });   
}
function survey_xchbj(deltheid,delthename){
    layer.confirm('编辑模式或修改配置', {
        btn: ['编辑模式','修改配置'] 
    }, function(){
        window.open('./?update='+deltheid+"|"+delthename);
    }, function(){
        layer.prompt({
            formType: 0,
            value: delthename,
            title: '输入新的试卷名'
        }, function(newname, index){
            const formData = new FormData();
            formData.append('exmatid', deltheid);
            formData.append('exmatname', newname);
            fetch('exam/updatetioname.php', {
                method: 'POST',
                body: formData
            })
            .then(function (response) {
                return response.json();
            }).then(function (data) {
                if(data.status === 200){
                    Qmsg.success('修改成功');
                }else{
                    Qmsg.error('修改失败');
                }
                layer.closeAll();
                setTimeout(function(){ location.reload(); }, 1000);
            })});
            
    });
}
function survey_seeino(surversid,survname){
    window.open('./?see='+surversid+"|"+survname);
}
function survey_toshareorsee(surversid){
    layer.open({
        type: 2,
        area: ['471px', '155px'],
        title: '分享链接',
        content: './otpage/geturl.html?cokkid='+surversid
    });
}
layui.use(function(){
    var layer = layui.layer;
    var util = layui.util;
    util.on('lay-on', {
        'layer-tipvip': function(){
            if(this.className === 'label-vip'){
                var valuesn = '您已开通会员';
            }else{
                var valuesn = '您未开通会员';
            }
            layer.tips(valuesn, this, {
                tips: [1, '#16b777']
              });
        }
      })
    });

function vipcdkchange(){
    layer.open({
        type: 2,
        area: ['471px', '155px'],
        title: '礼包码兑换',
        content: './otpage/cdk.html?cid='+useridn
    });
}

function vpnewexam(){
    layer.open({
        type: 2,
        area: ['871px', '500px'],
        title: '创建试卷',
        content: '../plugin/others/?mno=mr'
    });
}
function msgtoknow(){
    layer.open({
        type: 2,
        area: ['871px', '500px'],
        title: '消息中心',
        content: '../plugin/others/msg.php?type=t&userid='+userid
    });
}
function newgrjs_update(newname){
    fetch('./grxx/status.php?newname='+newname+'&userid='+useridn)
    .then(function (response) {
        return response.json();
    }).then(function (data) {
        layer.closeAll();
        if(data.status === 200){
            Qmsg.success('修改成功');
        }else{
            Qmsg.error('修改失败');
        }
    });
}
function newgrname_update(newname){
    fetch('./grxx/status2.php?newname='+newname+'&userid='+useridn)
    .then(function (response) {
        return response.json();
    }).then(function (data) {
        layer.closeAll();
        if(data.status === 200){
            Qmsg.success('修改成功');
        }else{
            Qmsg.error('修改失败');
        }
    });
}
document.getElementById('usertoonclick').addEventListener('click',function(){
    layer.confirm('修改名称或修改个性签名', {
        btn: ['修改名称','修改个性签名'] 
    }, function(){
    layer.confirm('确定要修改名称吗(已实名则无法修改)', {
        btn: ['是', '否']
    }, function(index){
        if(document.getElementById('smzt_namereal').innerText === '已实名认证'){
            layer.alert('已实名认证，无法修改');
            return;
        }
        layer.prompt({
            formType: 2,
            value: '',
            title: '输入新的用户名'
        }, function(newname, index){
            layer.close(index);
            newgrname_update(newname);
        });
    });
    }, function(index){
        layer.confirm('确定要修改个性签名吗', {
            btn: ['是','否'] 
        }, function(){
            layer.prompt({
                formType: 2,
                value: '',
                title: '输入新的个性签名'
            }, function(newname, index){
                layer.close(index);
                newgrjs_update(newname);
            });
        });    
    });
})